package com.learn.pay.common.util;

import java.util.Collection;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils  extends org.apache.commons.lang3.StringUtils {

    public static final String EMPTY_STRING = "";

    public static final String NULL = "null";

    public static final String SEP = " ";

    public static final String LINE_SEP = System.getProperty("line.separator");

    public static final String FILE_SEP = System.getProperty("file.separator");

    /**
     * 获取分隔符分隔的第一个字符串
     *
     * @param str
     * @param regex
     * @return
     */
    public static String getFirst(String str, String regex) {
        String[] array = split(str, regex);
        if (array.length <= 1) {
            return str;
        }
        return array[0];
    }

    /**
     * 获取分隔符分隔的最后一个字符串
     *
     * @param str
     * @param regex
     * @return
     */
    public static String getLast(String str, String regex) {
        String[] array = split(str, regex);
        if (array.length <= 1) {
            return str;
        }
        return array[array.length - 1];
    }

    public static String upperFirst(String str) {
        if (isEmpty(str)) {
            return str;
        }
        if (str.length() == 1) {
            return str.toUpperCase();
        }

        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }

    public static String lowerFirst(String str) {
        if (isEmpty(str)) {
            return str;
        }
        if (str.length() == 1) {
            return str.toLowerCase();
        }

        return str.substring(0, 1).toLowerCase() + str.substring(1);
    }

    public static String concat(Object... obj) {
        StringBuilder sb = new StringBuilder();
        for (Object each : obj) {
            sb.append(each);
        }
        return sb.toString();
    }

    public static String concatBySepAndColl(String sep, Collection<?> collection) {
        Iterator<?> iter = collection.iterator();
        int size = collection.size();
        int i = 0;
        StringBuilder sb = new StringBuilder();
        while (iter.hasNext()) {
            Object item = iter.next();
            sb.append(item);
            if (i + 1 < size) {
                sb.append(sep);
            }
            i++;
        }
        return sb.toString();
    }

    public static String chop(String str, int lastLen) {
        return str.substring(0, str.length() - lastLen);
    }

    public static boolean containChinese(String str) {
        String regEx = "[\\u4e00-\\u9fa5]+";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 下划线转驼峰
     * user_name  ---->  userName
     * house.user_name  ---->  userName
     * userName   --->  userName
     *
     * @param underlineName 带有下划线的名字
     * @return 驼峰字符串
     */
    public static String underlineToHump(String underlineName) {
        //截取下划线分成数组
        char[] charArray = underlineName.toCharArray();
        //判断上次循环的字符是否是"_"
        boolean underlineBefore = false;
        StringBuffer buffer = new StringBuffer();
        for (int i = 0, l = charArray.length; i < l; i++) {
            //判断当前字符是否是"_",如果跳出本次循环
            if (charArray[i] == 95) {
                underlineBefore = true;
            } else if (underlineBefore) {
                //如果为true，代表上次的字符是"_",当前字符需要转成大写
                buffer.append(charArray[i] -= 32);
                underlineBefore = false;
            } else { //不是"_"后的字符就直接追加
                buffer.append(charArray[i]);
            }
        }
        return buffer.toString();
    }

    /**
     * 驼峰转 下划线
     * userName  ---->  user_name
     * user_name  ---->  user_name
     *
     * @param humpName 驼峰命名
     * @return 带下滑线的String
     */
    public static String humpToUnderline(String humpName) {
        //截取下划线分成数组，
        char[] charArray = humpName.toCharArray();
        StringBuffer buffer = new StringBuffer();
        //处理字符串
        for (int i = 0, l = charArray.length; i < l; i++) {
            if (charArray[i] >= 65 && charArray[i] <= 90) {
                buffer.append("_").append(charArray[i] += 32);
            } else {
                buffer.append(charArray[i]);
            }
        }
        return buffer.toString();
    }

    /**
     * 删除多余空格
     * StringUtils.removeExtraSpace(" aa bb    cc  dd  ee  \n cs ")       = aa bb cc dd ee cs
     *
     * @param str
     * @return
     */
    public static String removeExtraSpace(String str) {
        str = str.trim().replaceAll("\\s{1,}", SPACE);
        return str;
    }


    public static void main(String[] args) {
        System.out.println(chop("abc", 1));
        System.out.println(containChinese("中232a"));
        System.out.println(containChinese("中中中中"));
        System.out.println(containChinese("a223中"));
        System.out.println(removeExtraSpace(" aa bb    cc  dd  ee  \n cs "));
    }
}
